package ctrl;


import service.Impl.IBookServiceImpl;
import service.Impl.IOrderLineServiceImpl;
import service.Impl.IOrderServiceImpl;
import service.Impl.IOrderStateServiceImpl;
import vo.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class showOrder extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        HttpSession httpSession=req.getSession();
        Client client = (Client) httpSession.getAttribute("client");
        if(client!=null){
            int userId=client.getId();

            //获得当前用户所有订单
            IOrderServiceImpl orderService=new IOrderServiceImpl();
            List<Order>orders=orderService.selectOrdersByUserId(userId);

            IOrderStateServiceImpl orderStateService=new IOrderStateServiceImpl();
            IOrderLineServiceImpl orderLineService=new IOrderLineServiceImpl();
            IBookServiceImpl bookService=new IBookServiceImpl();

            Map<UserOrder,List<UserOrderLine>>userOrders=new HashMap<>();

            for(Order order:orders){
                UserOrder userOrder=new UserOrder();

                int orderId=order.getId();
                OrderState orderState=orderStateService.selectOrderStateByOrderId(orderId);

                userOrder.setState(orderState.getState());
                userOrder.setTotalPrice(order.getTotalPrice());
                userOrder.setId(orderId);
                Timestamp timestamp=order.getDate();
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String date=dateFormat.format(timestamp);
                userOrder.setDate(date);

                //每个订单的具体信息
                List<OrderLine>orderLines= orderLineService.selectOrderLineByOrderId(orderId);
                List<UserOrderLine>userOrderLines=new ArrayList<>();
                for(OrderLine orderLine:orderLines){
                    UserOrderLine userOrderLine=new UserOrderLine();
                    int bookId=orderLine.getBookId();
                    Book book=bookService.selectBookById(bookId);
                    userOrderLine.setBookId(bookId);
                    userOrderLine.setQuanlity(orderLine.getQuanlity());
                    userOrderLine.setPrice(book.getPrice());
                    userOrderLine.setImgUrl(book.getImgUrl());
                    userOrderLine.setName(book.getName());
                    userOrderLine.setBriefIntro(book.getBriefIntro());

                    userOrderLines.add(userOrderLine);
                }
                userOrders.put(userOrder,userOrderLines);
            }

            req.setAttribute("userOrders",userOrders);
            req.getRequestDispatcher("/userOrders.jsp").forward(req,resp);

        }else{
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("{\"status\":102}");
        }

    }
}
